package com.wind.bdg.middlewarelearndemo.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Hello world!
 *
 */
public class App {
	static String url = "http://10.202.32.15/medicine/medicine/webNewsList.do?id=505798913";
	static String titleRegex = "<td>《*[0-9]*[\\u4e00-\\u9fa5]+.*?</td>";
	static String timeRegex = "<td>2020-.*?</td>";
//    public static void main( String[] args ) throws Exception{
//    	getUrl(url);
//    	getTime(timeRegex);
//    	getTitle(titleRegex);
//    }
        //获取标题
    public static List getTitle(String regex) throws Exception {
    	List list = new ArrayList<String>();
    	Document document = Jsoup.connect(url).get();
    	String doc = document.toString();
    	Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(doc);
		while(m.find()) {
			String res = m.group().replaceAll("</*td>", "");
//			System.out.println(res);
			list.add(res);
		}
		return list;
	}
        //获取时间
    public static List getTime(String regex) throws Exception {
    	List list = new ArrayList<String>();
    	Document document = Jsoup.connect(url).get();
    	String doc = document.toString();
    	Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(doc);
		while(m.find()) {
			String res = m.group().replaceAll("</*td>", "");
//			System.out.println(res);
			list.add(res);
		}
		return list;
	}
    
    public static List getUrl(String url1) throws Exception {
    	List list = new ArrayList<String>();
    	Document document = Jsoup.connect(url1).get();
    	//通过tag获取链接
		Elements elements = document.getElementsByTag("a");
		for(Element abc:elements) {
//				System.out.println(abc.attr("href"));
			list.add(abc.attr("href").replaceAll(".*?id=",""));
		}
		return list;
	}
    
}
